package com.sf.novatar.sample.redis;

import com.sf.iisp.bind.domain.BindingRecord;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by qi on 16-7-6.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/beans.xml"})
public class RedisMessageTestCase {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
//    public static void main(String args[]) {
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
//        RedisTemplate redisTemplate = (RedisTemplate) context.getBean("redisTemplate");
//        redisTemplate.convertAndSend("usr:binding", "{\n" +
//                "\t\"name\":\"zhaoqi\",\n" +
//                "\t\"age\" :15\n" +
//                "}");
//        redisTemplate.convertAndSend("usr:binding", "What's the fuck 1!!!!");
//        redisTemplate.convertAndSend("usr:binding", "What's the fuck 2!!!!");
//        redisTemplate.convertAndSend("usr:binding", "What's the fuck 3!!!!");
//        redisTemplate.convertAndSend("usr:unbinding", "What's the fuck !!!!");
//        redisTemplate.convertAndSend("usr:unbinding", "What's the fuck !!!!");
//        redisTemplate.convertAndSend("usr:binding", "What's the fuck4 !!!!");
//    }

    @Test
    public void testRedisList() {
//        binding-201607192036
        ListOperations<String, Object> listOps = redisTemplate.opsForList();
//        listOps.leftPush("binding-201607192036", "xxxxxxx");
//        Object obj =  listOps.rightPop("binding-201607192036");
//        System.out.println(obj);
//        Long object = listOps.size("binding-201607192105");
//        System.out.println("before :" + object);
//        Object obj =  listOps.rightPop("binding-201607192058");
//        System.out.println(obj);
        Long object = listOps.size("binding-201607192132");
        //listOps.trim("binding-201607192105", 0L, object - 1);
        for (long i = 0; i < object; i++) {

            BindingRecord br = (BindingRecord) listOps.leftPop("binding-201607192124");
            System.out.println(br.getCourierPhone());

        }
//        redisTemplate.delete("binding-201607192124");
//        object = listOps.size("binding-201607192105");
        System.out.println(object);
    }
}
